<?php

namespace App\Http\Controllers\Admin\Sponsor;

use App\Exceptions\SponsorException;
use App\Services\Admin\Sponsor\SponsorGoodsService;
use Exception;
use Illuminate\Http\JsonResponse;

class SponsorGoodsController extends BaseSponsorController
{

    protected SponsorGoodsService $sponsorGoodsService;

    public function __construct()
    {
        parent::__construct();
        $this->sponsorGoodsService = $this->loadService('Admin\Sponsor\SponsorGoodsService');
    }

    /**
     * 认养商品列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $list = $this->sponsorGoodsService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 认养商品详情
     * @return JsonResponse
     * @throws SponsorException
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->sponsorGoodsService->detail($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 创建认养商品
     * @return JsonResponse
     * @throws Exception
     */
    public function create(): JsonResponse
    {
        $rule = [
            'categoryId' => 'required|integer|min:1',
            'provinceId' => 'required|integer|min:1',
            'cityId' => 'required|integer|min:1',
            'regionId' => 'required|integer|min:1',
            'name' => 'required|string|min:1',
            'price' => 'required|string|min:1',
            'coverImg' => 'required|string|min:1',
            'startTime' => 'required|string|min:1',
            'cycle' => 'required|integer|min:1',
            'quantity' => 'required|integer|min:1',
            'isRecommend' => 'required|integer|in:0,1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorGoodsService->create($this->params);
        return outputJsonResult();
    }

    /**
     * 更新认养商品
     * @return JsonResponse
     * @throws Exception
     */
    public function update(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorGoodsService->update($this->params);
        return outputJsonResult();
    }

    /**
     * 删除认养商品
     * @return JsonResponse
     * @throws SponsorException
     */
    public function delete(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorGoodsService->delete($this->params);
        return outputJsonResult();
    }

    /**
     * 上下架
     * @return JsonResponse
     * @throws SponsorException
     */
    public function release(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'status' => 'required|integer|in:0,1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorGoodsService->release($this->params);
        return outputJsonResult();
    }

    /**
     * 推荐/取消推荐
     * @return JsonResponse
     * @throws SponsorException
     */
    public function recommend(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'status' => 'required|integer|in:0,1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorGoodsService->recommend($this->params);
        return outputJsonResult();
    }
}
